﻿<cfscript>
/** 
* @hint "Entity: ProgramTask, CommonTask"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.taskListener" 
	output=false 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "更新指定学期的教学任务选课人数"
	* 
	* @event "MachII Event Object"
	*/
	public void function flushTaskChoosen( required MachII.framework.Event event ) output=false {
		
		var termId = event.getArg("TermID");
		
		var sql = ""; 
		var queryObj = ""; 
		
		var i = 1;
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
		
			/* 计划课程人数刷新 */
			sql = "SELECT tsk_id FROM t_task WHERE term_id = :termId ";
			
			queryObj = new Query( datasource=application.dnsMaster ); 
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" ); 
			
			var rs_progTask = queryObj.execute( sql=sql ).getResult();
			
			for ( i=1; i LTE rs_progTask.recordcount; i++ ){ 
			
				sql = "SELECT count(*) countCor FROM t_student_course WHERE task_id = :taskId ";
			
				queryObj = new Query( datasource=application.dnsMaster ); 
				queryObj.addParam( name="taskId", value=rs_progTask["tsk_id"][i], cfsqltype="cf_sql_varchar" ); 
				
				var rs_chooserCount = queryObj.execute( sql=sql ).getResult();
				
				sql = "UPDATE t_task SET task_chooesd = :realSize WHERE tsk_id = :taskId "; 
				
				queryObj = new Query( datasource=application.dnsMaster ); 
				queryObj.addParam( name="realSize", value=rs_chooserCount.countCor, cfsqltype="cf_sql_varchar" ); 
				queryObj.addParam( name="taskId", value=rs_progTask["tsk_id"][i], cfsqltype="cf_sql_varchar" ); 
				
				queryObj.execute( sql=sql );
				
			}
			
			sql = "SELECT tsk_id FROM t_common_task WHERE term_id = :termId ";
			
			queryObj = new Query( datasource=application.dnsMaster ); 
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" ); 
			
			var rs_commonTask = queryObj.execute( sql=sql ).getResult();
			
			
			for ( i=1; i LTE rs_commonTask.recordcount; i++ ) {
				
				sql = "SELECT count(*) countCor FROM t_student_course WHERE task_id = :taskId ";
			
				queryObj = new Query( datasource=application.dnsMaster ); 
				queryObj.addParam( name="taskId", value=rs_commonTask["tsk_id"][i], cfsqltype="cf_sql_varchar" ); 
				
				var rs_chooserCount = queryObj.execute( sql=sql ).getResult();
				
				sql = "UPDATE t_common_task SET task_chooesd = :realSize WHERE tsk_id = :taskId "; 
				
				queryObj = new Query( datasource=application.dnsMaster ); 
				queryObj.addParam( name="realSize", value=rs_chooserCount.countCor, cfsqltype="cf_sql_varchar" ); 
				queryObj.addParam( name="taskId", value=rs_commonTask["tsk_id"][i], cfsqltype="cf_sql_varchar" ); 
				
				queryObj.execute( sql=sql );
				
			}
			
			sql = "SELECT tsk_id FROM t_sport_task WHERE term_id = :termId ";
			
			queryObj = new Query( datasource=application.dnsMaster ); 
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" ); 
			
			var rs_sportTask = queryObj.execute( sql=sql ).getResult();
			
			
			for ( i=1; i LTE rs_sportTask.recordcount; i++ ) {
				
				sql = "SELECT count(*) countCor FROM t_student_course WHERE task_id = :taskId ";
			
				queryObj = new Query( datasource=application.dnsMaster ); 
				queryObj.addParam( name="taskId", value=rs_sportTask["tsk_id"][i], cfsqltype="cf_sql_varchar" ); 
				
				var rs_chooserCount = queryObj.execute( sql=sql ).getResult();
				
				sql = "UPDATE t_sport_task SET task_chooesd = :realSize WHERE tsk_id = :taskId "; 
				
				queryObj = new Query( datasource=application.dnsMaster ); 
				queryObj.addParam( name="realSize", value=rs_chooserCount.countCor, cfsqltype="cf_sql_varchar" ); 
				queryObj.addParam( name="taskId", value=rs_sportTask["tsk_id"][i], cfsqltype="cf_sql_varchar" ); 
				
				queryObj.execute( sql=sql );
		
			}
			
			sql = "SELECT tsk_id FROM t_reopen_task WHERE term_id = :termId ";
			
			queryObj = new Query( datasource=application.dnsMaster ); 
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" ); 
			
			var rs_openTask = queryObj.execute( sql=sql ).getResult();
						
			for ( i=1; i LTE rs_openTask.recordcount; i++ ) {
				
				sql = "SELECT count(*) countCor FROM t_student_course WHERE task_id = :taskId ";
			
				queryObj = new Query( datasource=application.dnsMaster ); 
				queryObj.addParam( name="taskId", value=rs_openTask["tsk_id"][i], cfsqltype="cf_sql_varchar" ); 
				
				var rs_chooserCount = queryObj.execute( sql=sql ).getResult();
				
				sql = "UPDATE t_reopen_task SET task_chooesd = :realSize WHERE tsk_id = :taskId "; 
				
				queryObj = new Query( datasource=application.dnsMaster ); 
				queryObj.addParam( name="realSize", value=rs_chooserCount.countCor, cfsqltype="cf_sql_varchar" ); 
				queryObj.addParam( name="taskId", value=rs_openTask["tsk_id"][i], cfsqltype="cf_sql_varchar" ); 
				
				queryObj.execute( sql=sql );
		
			}
		
		}
		
	}

}
</cfscript>